package cn.itsource.hbc.order.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.hbc.order.domain.OrderAddress;
import cn.itsource.hbc.order.domain.OrderAdopt;

import cn.itsource.hbc.order.mapper.OrderAddressMapper;
import cn.itsource.hbc.order.mapper.OrderAdoptMapper;
import cn.itsource.hbc.order.service.IOrderAdaptService;
import cn.itsource.hbc.pay.contants.BusinessContants;
import cn.itsource.hbc.pay.domain.PayBill;
import cn.itsource.hbc.pay.mapper.PayBillMapper;
import cn.itsource.hbc.pay.service.IPayBillService;
import cn.itsource.hbc.pet.domain.Pet;
import cn.itsource.hbc.pet.mapper.PetMapper;
import cn.itsource.hbc.quartz.domain.QuartzJobInfo;
import cn.itsource.hbc.quartz.service.IQuartzService;
import cn.itsource.hbc.user.domain.User;
import cn.itsource.hbc.user.domain.UserAddress;
import cn.itsource.hbc.user.mapper.UserAddressMapper;
import cn.itsource.utils.AjaxResult;
import cn.itsource.utils.CodeGenerateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class OrderAdaptServiceImpl extends BaseServiceImpl<OrderAdopt> implements IOrderAdaptService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private OrderAdoptMapper orderAdoptMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IPayBillService payBillService;

    @Autowired
    private IQuartzService quartzService;

    @Override
    @Transactional
    public String createOrder(Map<String, String> map, User user) {
        // 把前端传回来的参数获取出来
        Long pet_id = Long.valueOf(map.get("pet_id"));
        Long address_id = Long.valueOf(map.get("address_id"));
        String payment_way = map.get("payment_way");
        // 通过pet_id获取到宠物对象
        Pet pet = petMapper.loadByIdOfFront(pet_id);

        System.out.println("宠物"+pet);
        pet.setState(0);
        pet.setOffsaletime(new Date());
        // 更新数据
        petMapper.update(pet);

        // 组装order订单对象
        OrderAdopt orderAdopt = createOrderAdopt(pet,user);
        // 操作数据库
        orderAdoptMapper.save(orderAdopt);
        //-----------------------------------------
        // 获取userid拿到地址对象  Long address_id
        UserAddress userAddress = userAddressMapper.loadById(address_id);
        // 将userAddress对象转化为orderAddress对象
        OrderAddress orderAddress =userAddress2orderAddress(userAddress);
//        private Date createTime;
//        private Date updateTime;
//        private Long order_id;
        orderAddress.setOrder_id(orderAdopt.getId());
//        private String orderSn;
        orderAddress.setOrderSn(orderAdopt.getOrderSn());
//        private String contacts;
//        private String areaCode;
//        private String address;
//        private String fullAddress;
//        private String phone;
//        private String phoneBack;
//        private String tel;
//        private String postCode;
//        private String email;
        orderAddressMapper.save(orderAddress);
//----------------------------------------------------
        PayBill payBill = createPayBill(orderAdopt);
        // 设置支付方式
        payBill.setPayChannel(payment_way);
        // 设置类型
        payBill.setBusinessType(BusinessContants.ORDER_ADOPT);
        // 设置key
        payBill.setBusinessKey(orderAdopt.getId());


        // 操作数据库，保存订单
        payBillService.save(payBill);

        String result = payBillService.pay(payBill);

        // 添加定时器任务
        QuartzJobInfo quartzJobInfo = new QuartzJobInfo();
        // 设置定时器名
        quartzJobInfo.setJobName(payBill.getOrderSn());
        Map<String,Object> param = new HashMap<>();
        param.put("orderSn",payBill.getOrderSn());
        // 设置携带的参数
        quartzJobInfo.setParams(param);
        // 设置触发时间
        quartzJobInfo.setFireDate(payBill.getLastPayTime());
        quartzService.addJob(quartzJobInfo);
        return result;

    }

    @Override
    public OrderAdopt queryByOrderSn(String orderSn) {
        return orderAdoptMapper.queryByOrderSn(orderSn);
    }

    private PayBill createPayBill(OrderAdopt orderAdopt) {
        // 创建支付清单对象
        PayBill payBill = new PayBill();
//        private String digest;
        payBill.setDigest(orderAdopt.getDigest());
//        private BigDecimal money;
        payBill.setMoney(orderAdopt.getPrice());
//        private Integer state;
        payBill.setState(0);
//        private Date lastPayTime;
        payBill.setLastPayTime(orderAdopt.getLastPayTime());
//        private String payChannel;
//        private Date createTime;
        payBill.setCreateTime(new Date());
//        private Date updateTime;
//        private Long user_id;
        payBill.setUser_id(orderAdopt.getUser_id());
//        private String nickName;
//        private String unionPaySn;
//        private String businessType;
//        private Long businessKey;
//        private Long shop_id;
        payBill.setShop_id(orderAdopt.getShop_id());
//        private String orderSn;
        payBill.setOrderSn(orderAdopt.getOrderSn());

        return payBill;
    }

    // 封装地址订单对象
    private OrderAddress userAddress2orderAddress(UserAddress userAddress) {
        OrderAddress orderAddress = new OrderAddress();
        // 复制拷贝数据
        BeanUtils.copyProperties(userAddress,orderAddress);
        // 处理特殊数据
        orderAddress.setCreateTime(new Date());
        return orderAddress;
    }

    // 封装订单对象
    private OrderAdopt createOrderAdopt(Pet pet, User user) {
        OrderAdopt orderAdopt = new OrderAdopt();

//        private String digest;
        orderAdopt.setDigest("宠物订单");
//        private Integer state;
        // 设置下架状态
        orderAdopt.setState(0);
//        private BigDecimal price;
        orderAdopt.setPrice(pet.getSaleprice());
//        private String orderSn;
        // 使用工具类获取订单编号随机数
        String orderSn = CodeGenerateUtils.generateOrderSn(user.getId());
        orderAdopt.setOrderSn(orderSn);

//        private Date lastPayTime = new Date();
        orderAdopt.setLastPayTime(new Date(System.currentTimeMillis()+30*60*1000));
//        private Date lastConfirmTime = new Date();
//        private Long pet_id;
        orderAdopt.setPet_id(pet.getId());
//        private Long user_id;
        orderAdopt.setUser_id(user.getId());
//        private Long shop_id;
        orderAdopt.setShop_id(pet.getShop_id());
//        private Long address_id;

        return orderAdopt;
    }

}

